JavaScript ecma6 将普通函数更改为箭头函数
全部标签 我有一个包含可滚动内容的div,在某个scrollTop值返回到顶部。varcontainer=document.getElementById('container');functionscroll_function(){varnew_position_top=container.scrollTop;if(new_position_top>600){container.scrollTop=0;}}container.addEventListener('scroll',scroll_function);#container{width:300px;height:300px;overflo
在此代码中,原型(prototype)仍然可以更改。如何防止对原型(prototype)进行更改?vara={a:1}varb={b:1}varc=Object.create(a)Object.getPrototypeOf(c)//ac.__proto__=b;Object.getPrototypeOf(c)//bvard=Object.create(null)Object.getPrototypeOf(d)//nulld.__proto__=b;Object.getPrototypeOf(d)//null 最佳答案 HowIcan
我在Angular2中的一个组件中遇到问题,因为“this”在我的一个组件中绑定(bind)到错误的上下文。我有其他组件没有发生此问题,但我看不出有什么区别。这是我的代码:组件:import{Component,Input}from'@angular/core';import{FilesService}from"./services/files.service";@Component({selector:'my-app',moduleId:module.id,templateUrl:'/app/views/app.html'})exportclassAppComponent{openF
1)调用varobj={num:2};varadd=function(a){returnthis.num+a;};add.call(obj,1);//function.call(obj,arg)2)调用对象的链构造函数。varProduct=function(name,price){this.name=name;this.price=price;}varFood=function(name,price){Product.call(this,name,price);//我目前正在研究javascriptoop,我找到了一个关于Function.prototype.call()链构造函数的
下面两个JavaScript函数有什么区别?我知道用var声明的变量在函数内部是局部的,如果用this`关键字声明,则会暴露给外部词。之间还有其他区别吗?functionstudent(param1,param2,param3){this.name=param1;this.age=param2;this.address=param3;}和functionstudent(param1,param2,param3){varname=param1;varage=param2;varaddress=param3;} 最佳答案 简短回答:您将
我正在尝试编写一个函数,它接受一个正整数并返回包含相同数字的下一个较小的正整数,如果没有包含相同数字的较小数字则返回-1。Forexample:nextSmaller(21)==12nextSmaller(531)==513nextSmaller(2071)==2017我写了一个解决这个问题的代码,但我真的不知道如何进一步优化它。请你帮助我好吗?它在repl.it上运行得相当快,但是当我提交它时,它说它需要超过1200毫秒并且不允许我提交它,即使所有测试都通过了。functionnextSmaller(n){varnArray=n.toString().split("")varmini
我觉得MDN文档或其他东西中可能遗漏了一些非常简单的东西,但我已经挖掘了一段时间,但我没有任何线索。有没有办法以类似于方法的方式调用函数?这基本上就是我想要做的:functionaddItem(itemName,quality,quantity/*,arr*/){arr.push([itemName,quality,quantity]);}varsomeArr=[['item',1,1]];someArr.addItem('someOtherItem',2,3);//someArr===[['item',1,1],['someOtherItem',2,3]]现在,请注意,我并不是要创建
我有一个功能,我想使用chrome.tabs.executeScript在页面中执行,从浏览器操作弹出窗口运行。权限设置正确,并且可以正常使用同步回调:chrome.tabs.executeScript(tab.id,{code:`(function(){//Dolotsofthingsreturntrue;})()`},r=>console.log(r[0]));//Logstrue问题是我要调用的函数要经过几个回调,所以我想使用async和await:chrome.tabs.executeScript(tab.id,{code:`(asyncfunction(){//Dolotso
我有一个这样的数组:vararr=[];arr=[['red',685],['green',210],['blue',65]];我还有两个变量:varcolor='blue';varnumber=21;我要做的就是检查arr的每个嵌套数组的第一项,然后更新它的第二项或为其创建一个新数组。这里是一些例子:输入:varcolor='blue';varnumber=21;预期输出:arr=[['red',685],['green',210],['blue',21]];输入:varcolor='yellow';varnumber=245;预期输出:arr=[['red',685],['gree
我想使用jQuery克隆div的内容,但是在使用appendTo函数之前,我想从复制的内容中删除原始内容中的类。当我从克隆中删除类时,它们也会从原始类中删除。我的代码是:$('.carousel.item').each(function(){varnext=$(this).next();if(!next.length){next=$(this).siblings(':first');}next.children(':first-child').clone().appendTo($(this));next.children(':first-child').addClass('col-sm